﻿<!DOCTYPE html>
<html>
<head>
  <title>Scroll Modes GoJS Sample</title>
  <!-- Copyright 1998-2021 by Northwoods Software Corporation. -->
  <meta name="description" content="Infinite scrolling and custom limits on Diagram position and scale." />
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <script src="../release/go.js"></script>
  <script src="../assets/js/goSamples.js"></script>  <!-- this is only for the GoJS Samples framework -->
  <script id="code">
    function init() {
      if (window.goSamples) goSamples();  // init for these samples -- you don't need to call this
      var $ = go.GraphObject.make;  // for conciseness in defining templates

      myDiagram = $(go.Diagram, "myDiagramDiv",  // create a Diagram for the DIV HTML element
        {
          minScale: 0.25,  // so that the contents and the grid cannot appear too small
          grid: $(go.Panel, "Grid",
            $(go.Shape, "LineH", { stroke: "gray", strokeWidth: .5 }),
            $(go.Shape, "LineH", { stroke: "darkslategray", strokeWidth: 1.5, interval: 10 }),
            $(go.Shape, "LineV", { stroke: "gray", strokeWidth: .5 }),
            $(go.Shape, "LineV", { stroke: "darkslategray", strokeWidth: 1.5, interval: 10 })
          ),
          "draggingTool.isGridSnapEnabled": true,
          "undoManager.isEnabled": true  // enable undo & redo
        });

      myDiagram.nodeTemplate =
        $(go.Node, "Auto",  // the Shape will go around the TextBlock
          $(go.Shape, "RoundedRectangle", { strokeWidth: 0 },
            // Shape.fill is bound to Node.data.color
            new go.Binding("fill", "color")),
          $(go.TextBlock,
            { margin: 8 },  // some room around the text
            // TextBlock.text is bound to Node.data.key
            new go.Binding("text", "key"))
        );

      // create the model data that will be represented by Nodes and Links
      myDiagram.model = new go.GraphLinksModel(
        [
          { key: "Alpha", color: "lightblue" },
          { key: "Beta", color: "orange" },
          { key: "Gamma", color: "lightgreen" },
          { key: "Delta", color: "pink" }
        ],
        [
          { from: "Alpha", to: "Beta" },
          { from: "Alpha", to: "Gamma" },
          { from: "Beta", to: "Beta" },
          { from: "Gamma", to: "Delta" },
          { from: "Delta", to: "Alpha" }
        ]);

      function positionfunc(diagram, pos) {
        var size = diagram.grid.gridCellSize;
        return new go.Point(
          Math.round(pos.x / size.width) * size.width,
          Math.round(pos.y / size.height) * size.height);
      }

      function scalefunc(diagram, scale) {
        var oldscale = diagram.scale;
        if (scale > oldscale) {
          return oldscale + 0.25;
        } else if (scale < oldscale) {
          return oldscale - 0.25;
        }
        return oldscale;
      }

      var infscroll = document.getElementById('infscroll');
      infscroll.addEventListener('change', function(e) {
        myDiagram.startTransaction('change scroll mode');
        myDiagram.scrollMode = infscroll.checked ? go.Diagram.InfiniteScroll : go.Diagram.DocumentScroll;
        myDiagram.commitTransaction('change scroll mode');
      });

      var poscomp = document.getElementById('poscomp');
      poscomp.addEventListener('change', function(e) {
        myDiagram.startTransaction('change position computation');
        myDiagram.positionComputation = poscomp.checked ? positionfunc : null;
        myDiagram.commitTransaction('change position computation');
      });

      var scalecomp = document.getElementById('scalecomp');
      scalecomp.addEventListener('change', function(e) {
        myDiagram.startTransaction('change scale computation');
        myDiagram.scaleComputation = scalecomp.checked ? scalefunc : null;
        myDiagram.commitTransaction('change scale computation');
      });

    }
  </script>
</head>
<body onload="init()">
<div id="sample">

  <div id="myDiagramDiv" style="border: solid 1px black; width:400px; height:400px"></div>
  <p>
    <label><input id="infscroll" type="checkbox" />Enable Infinite Scrolling, setting <a>Diagram.scrollMode</a></label>
    <pre>
      myDiagram.scrollMode = checked ? go.Diagram.InfiniteScroll : go.Diagram.DocumentScroll;
    </pre>
  </p>

  <p>
    <label><input id="poscomp" type="checkbox" />Enable <a>Diagram.positionComputation</a> function</label>
    <pre>
      function positionfunc(diagram, pos) {
        var size = diagram.grid.gridCellSize;
        return new go.Point(
          Math.round(pos.x / size.width) * size.width,
          Math.round(pos.y / size.height) * size.height);
      }
    </pre>
  </p>

  <p>
    <label><input id="scalecomp" type="checkbox" />Enable <a>Diagram.scaleComputation</a> function</label>
    <pre>
      function scalefunc(diagram, scale) {
        var oldscale = diagram.scale;
        if (scale > oldscale) {
          return oldscale + 0.25;
        } else if (scale < oldscale) {
          return oldscale - 0.25;
        }
        return oldscale;
      }
    </pre>
  </p>

  <p>This demonstrates new scrolling and scaling options available in <strong>GoJS 1.5</strong>.</p>

</div>
</body>
</html>